package content

import (
	"bytes"
	"context"
	"time"

	"gddgame.cc/galaxy/common/namespaces/cooperate"
	"gddgame.cc/galaxy/project/breeder/breeder/cooperated"
	"gddgame.cc/galaxy/utils"
)

const (
	Version string = "version"
)

type AutoPutContent struct {
	*cooperated.TypeMap
}

func (content *AutoPutContent) OnStart(master bool, proxy cooperate.MapProxy, context context.Context, data cooperate.PropsInstance) error {
	if !master {
		return nil
	}
	firstList := []cooperate.PropsInstance{}
	secondList := []cooperate.PropsInstance{}
	thirdList := []cooperate.PropsInstance{}
	tt := time.NewTicker(time.Second * 10)
	go func() {
		for {
			select {
			case <-tt.C:
				//fmt.Println("auto ticker")
				// 获取各个势力的投放点
				list := proxy.ListDepend(proxy, cooperate.ManageType, "put", cooperated.Key+cooperated.NodeDataType)
				version := data.ToInt64(Version)
				//fmt.Println(proxy.GetMapNoId(), version, proxy.TaskTime(), len(list))
				if version > 0 && version > proxy.TaskTime() {
					continue
				}
				updateData := cooperate.OperateContent{
					MapNoId: proxy.GetMapNoId(),
					Target:  data.Data(),
					Operates: []cooperate.OperateData{
						cooperate.NewOperateData(cooperate.BatchUpdateOperate, &cooperate.UpdateOperateData{
							Data: *data.Data(),
							Operates: []cooperate.UpdateOperateContent{{
								Operate: cooperate.ChangeData,
								Key:     Version,
								Value:   proxy.CurrentTime(),
							}},
						}),
					},
				}
				proxy.PushOperate(updateData, 0)
				for _, item := range list {
					flag := false
					identity := item.ToInt(cooperated.Identity)
					//fmt.Println("put", item.GetId(), item.ToBytes(cooperated.Position))
					id := utils.ToBytes(item.GetId())
					switch identity {
					case cooperated.FirstIdentityType:
						for _, k := range firstList {
							if bytes.Equal(id, utils.ToBytes(k.GetId())) {
								flag = true
								break
							}
						}
						if flag {
							continue
						}
						firstList = append(firstList, item)
					case cooperated.SecondIdentityType:
						for _, k := range secondList {
							if bytes.Equal(id, utils.ToBytes(k.GetId())) {
								flag = true
								break
							}
						}
						if flag {
							continue
						}
						secondList = append(secondList, item)
					case cooperated.ThirdIdentityType:
						for _, k := range thirdList {
							if bytes.Equal(id, utils.ToBytes(k.GetId())) {
								flag = true
								break
							}
						}
						if flag {
							continue
						}
						thirdList = append(thirdList, item)
					}
				}

				t := content.CurrentTime()
				if len(firstList) < 5 {
					position := content.RandomPosition(0, false, false, t)
					//fmt.Println("first put", proxy.GetMapNoId(), position)
					content.OnQuery(proxy, 0, cooperated.FirstIdentityType, cooperated.ResetNode, []interface{}{"put", position})
				}
				if len(secondList) < 5 {
					position := content.RandomPosition(0, false, false, t)
					//fmt.Println("second put", proxy.GetMapNoId(), position)
					content.OnQuery(proxy, 0, cooperated.SecondIdentityType, cooperated.ResetNode, []interface{}{"put", position})
				}
				if len(thirdList) < 5 {
					position := content.RandomPosition(0, false, false, t)
					//fmt.Println("third put", proxy.GetMapNoId(), position)
					content.OnQuery(proxy, 0, cooperated.ThirdIdentityType, cooperated.ResetNode, []interface{}{"put", position})
				}

				//fmt.Println("auto ai number", i)
			case <-context.Done():
				//fmt.Println("auto stop")
				tt.Stop()
				return
			}
		}

	}()
	return nil
}
func (content *AutoPutContent) OnStop() {

}

// 自定义定时器
func (content *AutoPutContent) Execute(task string) {

}
